package xhwl.hmo.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.List;

import xhwl.hmo.mall.R;
import xhwl.hmo.mall.http.bean.resp.FindResp;
import xhwl.hmo.mall.utils.AppUtil;
import xhwl.hmo.mall.utils.GlideRoundTransform;

import static xhwl.hmo.mall.R.id.tv_find_like;

/**
 * Created by admin on 2017-11-02.
 */

public class FindAdapter extends BaseAdapter {
    Context context;
    List<FindResp> mlist;

    public FindAdapter(Context context, List<FindResp> mlist) {
        this.context = context;
        this.mlist = mlist;
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_find, null);
            holder.tv_find_name = (TextView) convertView.findViewById(R.id.tv_find_name);
            holder.tv_find_time = (TextView) convertView.findViewById(R.id.tv_find_time);
            holder.img_find = (ImageView) convertView.findViewById(R.id.img_find);
            holder.tv_find_like = (TextView) convertView.findViewById(tv_find_like);
            holder.img_find_like = (ImageView) convertView.findViewById(R.id.img_find_like);
            holder.tv_find_pageview = (TextView) convertView.findViewById(R.id.tv_find_pageview);
            holder.linear_like= (LinearLayout) convertView.findViewById(R.id.linear_like);
            holder.img_find_transmit= (ImageView) convertView.findViewById(R.id.img_find_transmit);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.tv_find_name.setText(mlist.get(position).ACT_TITLE);
        holder.tv_find_time.setText(mlist.get(position).CREATE_DATE);
        Glide.with(context).load(mlist.get(position).ACT_PIC) .transform(new GlideRoundTransform(context)).placeholder(R.drawable.load).error(R.drawable.loadfail).into(holder.img_find);
        holder.tv_find_pageview.setText("浏览" + mlist.get(position).PAGE_VIEW + "次");
        holder.tv_find_like.setText(mlist.get(position).THUMBS_UP);
        if ("1".equals(mlist.get(position).THUMBS_FLAG)) {
            holder.img_find_like.setImageResource(R.drawable.have_thumb_up);
        } else {
            holder.img_find_like.setImageResource(R.drawable.praise);
        }
        holder.tv_find_like.setText(mlist.get(position).THUMBS_UP);

        holder.linear_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!AppUtil.isLogin()) {
                    AppUtil.startLoginActivity(context);
                    return;
                } else {

                    onItemAddClick.onItemClick(position);


                }
            }
        });
        holder.img_find_transmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MobclickAgent.onEvent(context,"onClick");
                UMImage umImage = new UMImage(context,mlist.get(position).ACT_PIC);
                String url="http://hmall.yk2020.com/index.php?m=Member&c=app&a=detail&artid="+mlist.get(position).RECORD_ID;
                new ShareAction((Activity) context).withText(mlist.get(position).ACT_TITLE).withTitle(mlist.get(position).ACT_TITLE).withMedia(umImage).withTargetUrl(url) .setDisplayList(SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.WEIXIN) .setCallback(umShareListener).open();
            }
        });
        return convertView;
    }

    public class ViewHolder {
        ImageView img_find;
        public ImageView img_find_like;
        ImageView img_find_transmit;
        TextView tv_find_name;
        TextView tv_find_time;
        TextView tv_find_pageview;
        public TextView tv_find_like;
        LinearLayout linear_like;
    }
    public static interface OnAddClickListener {
        // true add; false cancel
        public void onItemClick(int position); //传递boolean类型数据给activity
    }

    // add click callback
    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }
    UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
//			Toast.makeText(BZHomeActivity.this, platform + " 分享成功啦",
//					Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
//			Toast.makeText(BZHomeActivity.this, platform + " 分享失败啦",
//					Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//			Toast.makeText(BZHomeActivity.this, platform + " 分享取消了",
//					Toast.LENGTH_SHORT).show();
        }
    };

}
